package com.example.wastewater.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;

@Component
public class CustomMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        // 获取密码字段并进行加密处理
        Object password = getFieldValByName("password", metaObject);
        if (password != null) {
            String encryptedPassword = encryptPassword(password.toString());
            setFieldValByName("password", encryptedPassword, metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        // 获取密码字段并进行加密处理
        Object password = getFieldValByName("password", metaObject);
        if (password != null) {
            String encryptedPassword = encryptPassword(password.toString());
            setFieldValByName("password", encryptedPassword, metaObject);
        }
    }

    private String encryptPassword(String password) {
        // 使用MD5加密示例，实际项目中应使用更安全的加密方式
        return DigestUtils.md5DigestAsHex(password.getBytes());
    }
}

